package com.hezhi.framework.dao;

import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.hezhi.framework.dao.common.Extractor;
import com.hezhi.framework.dao.common.JdbcOprt;

public class BaseDaoImpl implements BaseDao {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6019322829697725719L;
	
	public static final int DEFAULT_PAGE_SIZE = 20;

	/**
	 * query the first record,then extractor the record to return a Object
	 * &lt;T&gt;
	 * 
	 * @param sql
	 * @param extractor
	 * @param args
	 * @return
	 */
	public <T> T get(Connection conn,final String sql, final Extractor<T> extractor,
			Object... args) {
		return JdbcOprt.get(conn,sql, extractor, args);
	}

	/**
	 * query all records,then extract record to object &lt;T&gt;, return a
	 * List&lt;T&gt;
	 * 
	 * @param sql
	 * @param extractor
	 * @param args
	 * @return
	 */
	public <T> List<T> list(Connection conn,String sql, Extractor<T> extractor,
			Object... args) {
		return JdbcOprt.list(conn,sql, extractor, args);
	}

	public <T> List<T> page(Connection conn,String sql, int pageNo,
			Extractor<T> extractor, Object... args) {
		return page(conn,sql, pageNo, DEFAULT_PAGE_SIZE, extractor, args);
	}

	/**
	 * query a page of records, then extract record to object &lt;T&gt;, return
	 * a List&lt;T&gt;
	 * 
	 * @param <T>
	 * @param pageNo
	 *            begin with 0
	 * @param pageSize
	 * @param sql
	 * @param extractor
	 * @param args
	 * @return
	 */
	public <T> List<T> page(Connection conn,String sql, int pageNo, int pageSize, 
			Extractor<T> extractor, Object... args) {
		return JdbcOprt.page(conn,pageNo, pageSize, sql, extractor, args);
	}

	/**
	 * 
	 * @param <K>
	 * @param <V>
	 * @param sql
	 * @param extractor
	 * @param keys
	 *            the last argument
	 * @param args
	 * @return
	 */
	public <K, V> Map<K, V> map(Connection conn,String sql, Extractor<V> extractor,
			Collection<K> keys, Object... args) {
		return JdbcOprt.map(conn,sql, extractor, keys, args);
	}

	/**
	 * update,insert,delete
	 * 
	 * @param sql
	 * @param args
	 * @return
	 */
	public int execute(Connection conn,String sql, Object... args) {
		return JdbcOprt.execute(conn,sql, args);
	}

	/**
	 * batch Execute
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchExecute(Connection conn,String sql, Object[]... batchArgs) {
		return JdbcOprt.batchExecute(conn,sql, batchArgs);
	}

	/**
	 * get sequence form DB by sequence name.
	 * 
	 * @param sequenceName
	 * @return
	 */
	public long sequence(Connection conn,String sequenceName) {
		return JdbcOprt.sequence(conn,sequenceName);
	}

	@Override
	public void createTable(Connection conn,String createTblSql) {
		JdbcOprt.execute(conn,createTblSql);
		
	}

	@Override
	public void dropTable(Connection conn,String dropTblSql) {
		JdbcOprt.execute(conn,dropTblSql);
	}
}
